﻿package  
{
	import com.event.EventZheng;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	/**
	 * ...
	 * @author zhengzihua
	 */
	public class Menu extends Sprite
	{
		public var menu:MenuMC;//菜单mc,界面上看到的就是这个mc
		private var maskMc:Sprite;//标题出现时候的遮罩
		private var maskSpeed:int = 20;//遮罩消失速度
		
		private var _level:int;//菜单的级别
		private var _count:int;//兄弟节点中,排行第几
		private var _parentMenu:Menu;//父菜单
		private var _xmlPath:XMLList;//节点路径
		private var _p:Point;//在场景中的坐标

		public function Menu(txt:String,level:int,count:int,parentMenu:Menu,xmlPath:XMLList) 
		{
			menu = new MenuMC(txt);
			maskMc = new Sprite();
			
			maskMc.graphics.beginFill(0xff0000);
			maskMc.graphics.drawRect(0, 0, menu.width+20, menu.height);
			maskMc.graphics.endFill();
			maskMc.cacheAsBitmap = true;
			maskMc.x = -menu.width;

			_count = count;
			_level = level
			_parentMenu = parentMenu;
			_xmlPath = xmlPath;
			addChild(menu);
			addChild(maskMc);
			menu.mask = maskMc;
			moveMask();
			menu.addEventListener(MouseEvent.CLICK, onClick);
			
			if(_level==3){
				addEnter();
			}
		}
		public function delMenu():void {//删除自己
			menu.removeEventListener(MouseEvent.CLICK, onClick);
			removeEventListener(Event.ENTER_FRAME, onEnter);
			parent.removeChild(this);
		}
		public function addEnter():void{
			addEventListener(Event.ENTER_FRAME, onEnter);
		}
		private function onEnter(e:Event):void{
			isShow();
		}
		public function isShow():void{
			if (y > StaticInfo.ROWSPACE_3 * 22 + StaticInfo.MENUEXCURSIONY || y < StaticInfo.MENUEXCURSIONY) {
				visible = false;
			}else {
				visible = true;
			}
		}
		private function moveMask():void {
			var id:int;
			id=setInterval(function ():void {
					if (maskMc.x <-maskSpeed) {
						maskMc.x += maskSpeed;
					}else {
						maskMc.x = 0;
						clearInterval(id);
						menu.mask = null;
						removeChild(maskMc);
						maskMc = null;
					}
				}, 30);
		}
		private function onClick(e:MouseEvent):void 
		{
			menuClick();
			var myEvent:EventZheng = new EventZheng(StaticInfo.MENUCLICK);
			dispatchEvent(myEvent);
		}
		private function menuClick():void {
			//DebugTrace.dtrace("code info Menu.as:点击效果.");
		}
		
		public function get parentMenu():Menu { return _parentMenu; }
		
		public function set parentMenu(value:Menu):void 
		{
			_parentMenu = value;
		}
		
		public function get level():int { return _level; }
		
		public function get count():int { return _count; }
		
		public function set level(value:int):void 
		{
			_level = value;
		}
		
		public function set count(value:int):void 
		{
			_count = value;
		}
		
		public function get p():Point { return _p; }
		
		public function get xmlPath():XMLList { return _xmlPath; }
		
		public function set xmlPath(value:XMLList):void 
		{
			_xmlPath = value;
		}
		
		public function set p(value:Point):void 
		{
			_p = value;
		}
		
	}
	
}